﻿using AutoMapper;
using CoreColution.Entity.Basics;
using CoreColution.EntityDto.Basics;
using CoreSolution.IService.Basics;
using CoreSolution.Tools;
using CoreSolution.Tools.WebResult;
using Microsoft.AspNetCore.Cors;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Net;
using System.Threading.Tasks;

namespace CoreSolution.Vip.Controllers
{

    [EnableCors("AllowAllOrigin")]
    [Route("api/Role")]
    [Produces("application/json")]
    //[ApiController] 不能采用，不然action 就不能以对象形式接受参数
    public class RoleContorller : ControllerBase
    {
        private readonly IRoleService _IRoleService;

        public RoleContorller(IRoleService iRoleService)
        {
            _IRoleService = iRoleService;
        }

        #region 后台操作
        /// <summary>
        /// 获取角色列表
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        [HttpGet]
        [Route("GetRoleInfoList")]
        public async Task<JsonResult> GetRoleInfoList(RoleSearch model)
        {
            var where = PredicateExtensions.True<Role>();
            if (!string.IsNullOrEmpty(model.Name))
            {
                where = where.And(x => x.Name.Contains(model.Name));
            }
            
            var UserInfo = _IRoleService.Where(where).OrderByDesc(t=>t.CreateDate).Skip((model.PageIndex - 1) * model.PageSize).Take(model.PageSize).ToList();
            return AjaxHelper.JsonResult(HttpStatusCode.OK, "获取成功", UserInfo);
        }

        /// <summary>
        /// 添加编辑角色
        /// </summary>
        /// <param name="model">实体类</param>
        /// <param name="TypeOper">操作:0.添加 1 编辑</param>
        /// <returns></returns>
        [HttpPost]
        [Route("AddEditRoleInfo")]
        public async Task<JsonResult> AddEditRoleInfo(RoleDto model)
        {
        
            var data = Mapper.Map<Role>(model);
            var Num = 0;
            var Msg = "";

            if (model.TypeOper == 0)
            {
                data.Id = Guid.NewGuid();
                Num = _IRoleService.Add(data);
                Msg = "添加成功";
            }
            else
            {
                Num = _IRoleService.Update(data);
                Msg = "修改成功";
            }
            _IRoleService.SaveChange(Num);
            return AjaxHelper.JsonResult(HttpStatusCode.OK, Msg);
        }

        /// <summary>
        /// 删除角色
        /// </summary>
        /// <param name="model">实体类</param>
        /// <param name="TypeOper">操作:0.添加 1 编辑</param>
        /// <returns></returns>
        [HttpPost]
        [Route("DeleteRoleInfo")]
        public async Task<JsonResult> DeleteRoleInfo(Guid Id)
        {
            var num = _IRoleService.Delete(Id);
            _IRoleService.SaveChange(num);
            return AjaxHelper.JsonResult(HttpStatusCode.OK, "删除成功");
        }

        /// <summary>
        /// 获取角色信息
        /// </summary>
        /// <param name="id">id</param>
        /// <returns></returns>
        [HttpGet]
        [Route("GetRoleInfo")]
        public async Task<JsonResult> GetRoleInfo(Guid Id)
        {
            var ReleInfo = _IRoleService.FirstOrDefault(t=>t.Id==Id);
            return AjaxHelper.JsonResult(HttpStatusCode.OK, "获取成功", ReleInfo);
        }
        #endregion

    }
}
